package com.inei.reportedeverificacion.view;

import com.inei.reportedeverificacion.R;
import com.inei.reportedeverificacion.controller.ReporteController;
import com.inei.reportedeverificacion.model.entity.Reporte;
import com.inei.reportedeverificacion.model.entity.ReporteTmp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Fragment_Seccion_2 extends Activity {

	// componentes
	private TextView tvC6_78_1, tvC6_79_1, tvC6_78_2, tvC6_79_2, tvC6_78_3,
			tvC6_79_3, tvC6_78_4, tvC6_79_4, tvC6_78_5, tvC6_79_5, tvC6_78_6,
			tvC6_79_6, tvC6_78_7, tvC6_79_7, tvC6_78_8, tvC6_79_8, tvC6_78_9,
			tvC6_79_9, tvC6_78_10, tvC6_79_10, tvC6_78_11, tvC6_79_11, tvC7_84,
			tvC7_85, tvC7_95, tvC8_97, tvC8_97_Cuantos;
	private Spinner spC6_78_1, spC6_79_1, spC6_78_2, spC6_79_2, spC6_78_3,
			spC6_79_3, spC6_78_4, spC6_79_4, spC6_78_5, spC6_79_5, spC6_78_6,
			spC6_79_6, spC6_78_7, spC6_79_7, spC6_78_8, spC6_79_8, spC6_78_9,
			spC6_79_9, spC6_78_10, spC6_79_10, spC6_78_11, spC6_79_11, spC7_84,
			spC7_85, spC7_95, spC8_97, spC8_97_Cuantos;

	// variables
	private ReporteController con;
	private Reporte rep;
	private ReporteTmp repTmp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_seccion_2);

		con = new ReporteController(getApplicationContext());

		IniciarComponentes();

		CargarDatos();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_frag2, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.mnGuardar:
			GuardarDatos();
			break;
		case R.id.mnSalir:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		Intent ir_frag1 = new Intent(this, Fragment_Seccion_1.class);
		ir_frag1.putExtras(getIntent().getExtras());
		startActivity(ir_frag1);
		finish();

		super.onBackPressed();
	}

	private void IniciarComponentes() {
		tvC6_78_1 = (TextView) findViewById(R.id.tvC6_78_1);
		tvC6_79_1 = (TextView) findViewById(R.id.tvC6_79_1);
		tvC6_78_2 = (TextView) findViewById(R.id.tvC6_78_2);
		tvC6_79_2 = (TextView) findViewById(R.id.tvC6_79_2);
		tvC6_78_3 = (TextView) findViewById(R.id.tvC6_78_3);
		tvC6_79_3 = (TextView) findViewById(R.id.tvC6_79_3);
		tvC6_78_4 = (TextView) findViewById(R.id.tvC6_78_4);
		tvC6_79_4 = (TextView) findViewById(R.id.tvC6_79_4);
		tvC6_78_5 = (TextView) findViewById(R.id.tvC6_78_5);
		tvC6_79_5 = (TextView) findViewById(R.id.tvC6_79_5);
		tvC6_78_6 = (TextView) findViewById(R.id.tvC6_78_6);
		tvC6_79_6 = (TextView) findViewById(R.id.tvC6_79_6);
		tvC6_78_7 = (TextView) findViewById(R.id.tvC6_78_7);
		tvC6_79_7 = (TextView) findViewById(R.id.tvC6_79_7);
		tvC6_78_8 = (TextView) findViewById(R.id.tvC6_78_8);
		tvC6_79_8 = (TextView) findViewById(R.id.tvC6_79_8);
		tvC6_78_9 = (TextView) findViewById(R.id.tvC6_78_9);
		tvC6_79_9 = (TextView) findViewById(R.id.tvC6_79_9);
		tvC6_78_10 = (TextView) findViewById(R.id.tvC6_78_10);
		tvC6_79_10 = (TextView) findViewById(R.id.tvC6_79_10);
		tvC6_78_11 = (TextView) findViewById(R.id.tvC6_78_11);
		tvC6_79_11 = (TextView) findViewById(R.id.tvC6_79_11);
		tvC7_84 = (TextView) findViewById(R.id.tvC7_84);
		tvC7_85 = (TextView) findViewById(R.id.tvC7_85);
		tvC7_95 = (TextView) findViewById(R.id.tvC7_95);
		tvC8_97 = (TextView) findViewById(R.id.tvC8_97);
		tvC8_97_Cuantos = (TextView) findViewById(R.id.tvC8_97_Cuantos);

		spC6_78_1 = (Spinner) findViewById(R.id.spC6_78_1);
		spC6_79_1 = (Spinner) findViewById(R.id.spC6_79_1);
		spC6_78_2 = (Spinner) findViewById(R.id.spC6_78_2);
		spC6_79_2 = (Spinner) findViewById(R.id.spC6_79_2);
		spC6_78_3 = (Spinner) findViewById(R.id.spC6_78_3);
		spC6_79_3 = (Spinner) findViewById(R.id.spC6_79_3);
		spC6_78_4 = (Spinner) findViewById(R.id.spC6_78_4);
		spC6_79_4 = (Spinner) findViewById(R.id.spC6_79_4);
		spC6_78_5 = (Spinner) findViewById(R.id.spC6_78_5);
		spC6_79_5 = (Spinner) findViewById(R.id.spC6_79_5);
		spC6_78_6 = (Spinner) findViewById(R.id.spC6_78_6);
		spC6_79_6 = (Spinner) findViewById(R.id.spC6_79_6);
		spC6_78_7 = (Spinner) findViewById(R.id.spC6_78_7);
		spC6_79_7 = (Spinner) findViewById(R.id.spC6_79_7);
		spC6_78_8 = (Spinner) findViewById(R.id.spC6_78_8);
		spC6_79_8 = (Spinner) findViewById(R.id.spC6_79_8);
		spC6_78_9 = (Spinner) findViewById(R.id.spC6_78_9);
		spC6_79_9 = (Spinner) findViewById(R.id.spC6_79_9);
		spC6_78_10 = (Spinner) findViewById(R.id.spC6_78_10);
		spC6_79_10 = (Spinner) findViewById(R.id.spC6_79_10);
		spC6_78_11 = (Spinner) findViewById(R.id.spC6_78_11);
		spC6_79_11 = (Spinner) findViewById(R.id.spC6_79_11);
		spC7_84 = (Spinner) findViewById(R.id.spC7_84);
		spC7_85 = (Spinner) findViewById(R.id.spC7_85);
		spC7_95 = (Spinner) findViewById(R.id.spC7_95);
		spC8_97 = (Spinner) findViewById(R.id.spC8_97);
		spC8_97_Cuantos = (Spinner) findViewById(R.id.spC8_97_Cuantos);
	}

	private void CargarDatos() {
		repTmp = new ReporteTmp();
		repTmp.setCLAVEVIVHOGAR(getIntent().getExtras().getString(
				"CLAVEVIVHOGAR"));
		repTmp = con.ReporteTmpBuscar(repTmp);

		if (repTmp != null) {
			if (repTmp.getC1P78_1() != 0) {
				tvC6_78_1.setText(rptaSiNo(repTmp.getC1P78_1()));
			}
			if (repTmp.getC1P79_1() != 0) {
				tvC6_79_1.setText(repTmp.getC1P79_1() + "");
			}
			if (repTmp.getC1P78_2() != 0) {
				tvC6_78_2.setText(rptaSiNo(repTmp.getC1P78_2()));
			}
			if (repTmp.getC1P79_2() != 0) {
				tvC6_79_2.setText(repTmp.getC1P79_2() + "");
			}
			if (repTmp.getC1P78_3() != 0) {
				tvC6_78_3.setText(rptaSiNo(repTmp.getC1P78_3()));
			}
			if (repTmp.getC1P79_3() != 0) {
				tvC6_79_3.setText(repTmp.getC1P79_3() + "");
			}
			if (repTmp.getC1P78_4() != 0) {
				tvC6_78_4.setText(rptaSiNo(repTmp.getC1P78_4()));
			}
			if (repTmp.getC1P79_4() != 0) {
				tvC6_79_4.setText(repTmp.getC1P79_4() + "");
			}
			if (repTmp.getC1P78_5() != 0) {
				tvC6_78_5.setText(rptaSiNo(repTmp.getC1P78_5()));
			}
			if (repTmp.getC1P79_5() != 0) {
				tvC6_79_5.setText(repTmp.getC1P79_5() + "");
			}
			if (repTmp.getC1P78_6() != 0) {
				tvC6_78_6.setText(rptaSiNo(repTmp.getC1P78_6()));
			}
			if (repTmp.getC1P79_6() != 0) {
				tvC6_79_6.setText(repTmp.getC1P79_6() + "");
			}
			if (repTmp.getC1P78_7() != 0) {
				tvC6_78_7.setText(rptaSiNo(repTmp.getC1P78_7()));
			}
			if (repTmp.getC1P79_7() != 0) {
				tvC6_79_7.setText(repTmp.getC1P79_7() + "");
			}
			if (repTmp.getC1P78_8() != 0) {
				tvC6_78_8.setText(rptaSiNo(repTmp.getC1P78_8()));
			}
			if (repTmp.getC1P79_8() != 0) {
				tvC6_79_8.setText(repTmp.getC1P79_8() + "");
			}
			if (repTmp.getC1P78_9() != 0) {
				tvC6_78_9.setText(rptaSiNo(repTmp.getC1P78_9()));
			}
			if (repTmp.getC1P79_9() != 0) {
				tvC6_79_9.setText(repTmp.getC1P79_9() + "");
			}
			if (repTmp.getC1P78_10() != 0) {
				tvC6_78_10.setText(rptaSiNo(repTmp.getC1P78_10()));
			}
			if (repTmp.getC1P79_10() != 0) {
				tvC6_79_10.setText(repTmp.getC1P79_10() + "");
			}
			if (repTmp.getC1P78_11() != 0) {
				tvC6_78_11.setText(rptaSiNo(repTmp.getC1P78_11()));
			}
			if (repTmp.getC1P79_11() != 0) {
				tvC6_79_11.setText(repTmp.getC1P79_11() + "");
			}
			if (repTmp.getC1P84() != 0) {
				tvC7_84.setText(rptaSiNo(repTmp.getC1P84()));
			}
			if (repTmp.getC1P85() != 0) {
				tvC7_85.setText(repTmp.getC1P85() + "");
			}
			if (repTmp.getC1P95() != 0) {
				tvC7_95.setText(rptaSiNo(repTmp.getC1P95()));
			}
			if (repTmp.getC1P97() != 0) {
				tvC8_97.setText(rptaSiNo(repTmp.getC1P97()));
			}
			if (repTmp.getT_05_DIG_CAP08_2() != 0) {
				tvC8_97_Cuantos.setText(repTmp.getT_05_DIG_CAP08_2() + "");
			}
		}

		rep = new Reporte();
		rep.setCLAVEVIVHOGAR(getIntent().getExtras().getString("CLAVEVIVHOGAR"));
		rep = con.ReporteBuscar(rep);

		if (rep != null) {
			if (rep.getC1P78_1() != 0) {
				spC6_78_1.setSelection(rep.getC1P78_1());
			}
			if (rep.getC1P79_1() != 0) {
				spC6_79_1.setSelection(rep.getC1P79_1());
			}
			if (rep.getC1P78_2() != 0) {
				spC6_78_2.setSelection(rep.getC1P78_2());
			}
			if (rep.getC1P79_2() != 0) {
				spC6_79_2.setSelection(rep.getC1P79_2());
			}
			if (rep.getC1P78_3() != 0) {
				spC6_78_3.setSelection(rep.getC1P78_3());
			}
			if (rep.getC1P79_3() != 0) {
				spC6_79_3.setSelection(rep.getC1P79_3());
			}
			if (rep.getC1P78_4() != 0) {
				spC6_78_4.setSelection(rep.getC1P78_4());
			}
			if (rep.getC1P79_4() != 0) {
				spC6_79_4.setSelection(rep.getC1P79_4());
			}
			if (rep.getC1P78_5() != 0) {
				spC6_78_5.setSelection(rep.getC1P78_5());
			}
			if (rep.getC1P79_5() != 0) {
				spC6_79_5.setSelection(rep.getC1P79_5());
			}
			if (rep.getC1P78_6() != 0) {
				spC6_78_6.setSelection(rep.getC1P78_6());
			}
			if (rep.getC1P79_6() != 0) {
				spC6_79_6.setSelection(rep.getC1P79_6());
			}
			if (rep.getC1P78_7() != 0) {
				spC6_78_7.setSelection(rep.getC1P78_7());
			}
			if (rep.getC1P79_7() != 0) {
				spC6_79_7.setSelection(rep.getC1P79_7());
			}
			if (rep.getC1P78_8() != 0) {
				spC6_78_8.setSelection(rep.getC1P78_8());
			}
			if (rep.getC1P79_8() != 0) {
				spC6_79_8.setSelection(rep.getC1P79_8());
			}
			if (rep.getC1P78_9() != 0) {
				spC6_78_9.setSelection(rep.getC1P78_9());
			}
			if (rep.getC1P79_9() != 0) {
				spC6_79_9.setSelection(rep.getC1P79_9());
			}
			if (rep.getC1P78_10() != 0) {
				spC6_78_10.setSelection(rep.getC1P78_10());
			}
			if (rep.getC1P79_10() != 0) {
				spC6_79_10.setSelection(rep.getC1P79_10());
			}
			if (rep.getC1P78_11() != 0) {
				spC6_78_11.setSelection(rep.getC1P78_11());
			}
			if (rep.getC1P79_11() != 0) {
				spC6_79_11.setSelection(rep.getC1P79_11());
			}
			if (rep.getC1P84() != 0) {
				spC7_84.setSelection(rep.getC1P84());
			}
			if (rep.getC1P85() != 0) {
				spC7_85.setSelection(rep.getC1P85());
			}
			if (rep.getC1P95() != 0) {
				spC7_95.setSelection(rep.getC1P95());
			}
			if (rep.getC1P97() != 0) {
				spC8_97.setSelection(rep.getC1P97());
			}
			if (rep.getT_05_DIG_CAP03_COUNT() != 0) {
				spC8_97_Cuantos.setSelection(rep.getT_05_DIG_CAP03_COUNT());
			}
		}
	}

	public void GuardarDatos() {
		rep.setC1P78_1(spC6_78_1.getSelectedItemPosition());
		rep.setC1P79_1(spC6_79_1.getSelectedItemPosition());
		rep.setC1P78_2(spC6_78_2.getSelectedItemPosition());
		rep.setC1P79_2(spC6_79_2.getSelectedItemPosition());
		rep.setC1P78_3(spC6_78_3.getSelectedItemPosition());
		rep.setC1P79_3(spC6_79_3.getSelectedItemPosition());
		rep.setC1P78_4(spC6_78_4.getSelectedItemPosition());
		rep.setC1P79_4(spC6_79_4.getSelectedItemPosition());
		rep.setC1P78_5(spC6_78_5.getSelectedItemPosition());
		rep.setC1P79_5(spC6_79_5.getSelectedItemPosition());
		rep.setC1P78_6(spC6_78_6.getSelectedItemPosition());
		rep.setC1P79_6(spC6_79_6.getSelectedItemPosition());
		rep.setC1P78_7(spC6_78_7.getSelectedItemPosition());
		rep.setC1P79_7(spC6_79_7.getSelectedItemPosition());
		rep.setC1P78_8(spC6_78_8.getSelectedItemPosition());
		rep.setC1P79_8(spC6_79_8.getSelectedItemPosition());
		rep.setC1P78_9(spC6_78_9.getSelectedItemPosition());
		rep.setC1P79_9(spC6_79_9.getSelectedItemPosition());
		rep.setC1P78_10(spC6_78_10.getSelectedItemPosition());
		rep.setC1P79_10(spC6_79_10.getSelectedItemPosition());
		rep.setC1P78_11(spC6_78_11.getSelectedItemPosition());
		rep.setC1P79_11(spC6_79_11.getSelectedItemPosition());
		rep.setC1P84(spC7_84.getSelectedItemPosition());
		rep.setC1P85(spC7_85.getSelectedItemPosition());
		rep.setC1P95(spC7_95.getSelectedItemPosition());
		rep.setC1P97(spC8_97.getSelectedItemPosition());
		rep.setC1P97_COUNT(spC8_97_Cuantos.getSelectedItemPosition());

		if (con.ReporteModificar(rep) > 0) {

			Intent ir_frag3 = new Intent(this, Fragment_Seccion_3.class);
			Bundle datos = getIntent().getExtras();
			ir_frag3.putExtras(datos);
			startActivity(ir_frag3);

			Toast.makeText(this, "Se actualizo con exito", Toast.LENGTH_SHORT)
					.show();

			finish();
		} else {
			Toast.makeText(this, "Error al actualizar", Toast.LENGTH_SHORT)
					.show();
		}
	}

	private String tabla01(int pos) {
		return getResources().getStringArray(R.array.tabla01)[pos];
	}

	private String tabla02(int pos) {
		return getResources().getStringArray(R.array.tabla02)[pos];
	}

	private String tabla03(int pos) {
		return getResources().getStringArray(R.array.tabla03)[pos];
	}

	private String rptaSiNo(int pos) {
		return getResources().getStringArray(R.array.rptaSiNo)[pos];
	}
}
